#ifndef BOXRENDERER_H
#define BOXRENDERER_H

#include <qopengl.h>

#include <QVector4D>
#include <QVector>

#include <qrender/renderer.h>

class QOpenGLShaderProgram;
class QOpenGLVertexArrayObject;
class QOpenGLBuffer;
class QOpenGLTexture;

class BoxRenderer : public Renderer
{
public:
    enum BoxType
    {
        BoxType_Plane = 0, ///< 画 6 个平面
        BoxType_Line,      ///< 只画边框线
    };

public:
    BoxRenderer();
    ~BoxRenderer();

    void setPose(const std::vector<double> &pose);
    void setShape(float length, float width, float height,
                  BoxType type = BoxType_Plane);
    void setColor(const QVector4D &color);
    void setTexture(const QString &texture_path);

protected:
    void create() override;
    void render(const CameraInfo &camera) override;
    void release() override;

private:
    void generateBoxVertices();
    void generateBoxPlaneIndices();
    void generateBoxLineIndices();
    void generateTexture();

    void generateColorProgram();
    void generateTextureProgram();

private:
    std::vector<double> pose_{ std::vector<double>(6, 0.) };
    float x_len_{ 0.0 };
    float y_len_{ 0.0 };
    float z_len_{ 0.0 };
    QVector4D color_{ 0.0, 1.0, 0.0, 1.0 };
    QString texture_path_;
    BoxType type_{ BoxType_Plane };

    bool data_changed_{ false };
    bool texture_changed_{ false };

    std::shared_ptr<QOpenGLShaderProgram> program_color_{ nullptr };
    std::shared_ptr<QOpenGLShaderProgram> program_texture_{ nullptr };
    std::shared_ptr<QOpenGLVertexArrayObject> vao_color_{ nullptr };
    std::shared_ptr<QOpenGLVertexArrayObject> vao_texture_{ nullptr };
    std::shared_ptr<QOpenGLBuffer> vbo_{ nullptr };
    std::shared_ptr<QOpenGLTexture> texture_{ nullptr };

    QVector<GLfloat> vertices_;
    QVector<GLuint> plane_indices_;
    QVector<GLuint> line_indices_;
};

#endif // BOXRENDERER_H
